package com.gtt.jr.repository.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.gtt.jr.constants.CommentsType;
import com.gtt.jr.repository.CommentsDao;
import com.gtt.jr.repository.domain.Comments;
import com.gtt.jr.repository.impl.base.BaseDaoImpl;

/**
 * Comments Dao Impl
 * 
 * @author Michael(gao12581@sina.com)
 * @date 2011-3-2 08:59:39
 * 
 */
@Service
public class CommentsDaoImpl extends BaseDaoImpl<Comments> implements
		CommentsDao {

	public List<Comments> loadAllByType(CommentsType type, Long entityId) {
		StringBuffer sql = new StringBuffer();
		sql.append("from Comments c");
		sql.append(" where c.id in( ");
		sql.append("select cb.comments.id from CommentsBridge cb");
		sql.append(" where cb.type=?");
		sql.append(" and cb.refId=?");
		sql.append(") and order by c.created desc");
		return find(Comments.class, sql.toString(), new Object[] { type,
				entityId });
	}

	// @Override
	// public List<Comments> loadAllChildrenByComments(List<String> ids) {
	// if (ids == null || ids.isEmpty()) {
	// return new ArrayList<Comments>();
	// }
	// String idstring = IDCollectionUtils.toString(ids);
	//
	// StringBuffer sql = new StringBuffer();
	// sql.append("from " + Comments.class.getName() + " c");
	// sql.append(" where c.parent.id in (" + idstring + ")");
	// sql.append(" and c.domainStatus=? order by c.created desc");
	// return commonDao.find(Comments.class, sql.toString(),
	// new Object[] { DomainStatus.NORMAL });
	// }
	//
	// @Override
	// public Pagination<Comments> loadCommentsByUserName(String userName,
	// CommentsType type, int pageSize, int pageNo, String sort, String dir) {
	// String ids = getEntityIds(userName, type);
	// return commonDao.findPage(Comments.class, getQueryString(ids),
	// new Object[] { type, DomainStatus.NORMAL }, pageSize, pageNo,
	// "c." + sort, dir);
	// }
	//
	// @Override
	// public long countCommentsByUser(String userName, CommentsType type) {
	// String ids = getEntityIds(userName, type);
	// return commonDao.count(getQueryString(ids), new Object[] { type,
	// DomainStatus.NORMAL });
	// }
	//
	// @Override
	// public Pagination<Comments> loadRepliesByUser(String userName,
	// CommentsType type, int pageSize, int pageNo, String sort, String dir) {
	// return null;
	// }
	//
	// @Override
	// public long countRepliesByUser(String userName, CommentsType type) {
	// return 0;
	// }
	//
	// @Override
	// public List<Comments> loadAllParentByComments(List<String> ids) {
	// if (ids == null || ids.isEmpty()) {
	// return new ArrayList<Comments>();
	// }
	// String idstring = IDCollectionUtils.toString(ids);
	//
	// StringBuffer sql = new StringBuffer();
	// sql.append("from " + Comments.class.getName() + " c1");
	// sql.append(" where c1.id in (select c2.parent.id from "
	// + Comments.class.getName() + " c2");
	// sql.append(" where c2.id in (" + idstring + "))");
	// sql.append(" order by c1.created desc");
	// return commonDao.find(Comments.class, sql.toString());
	// }
	//
	// private String getEntityIds(String userName, CommentsType type) {
	// String ids = null;
	// List<String> idList = new ArrayList<String>();
	// switch (type) {
	// case PRODUCT:
	// List<SiteItem> items = siteItemService
	// .findAllProductsByUser(userName);
	// for (SiteItem item : items) {
	// idList.add(item.getId());
	// }
	// break;
	// case COMPANY:
	// SiteCompany company = siteCompanyService.loadByUserName(userName);
	// idList.add(company.getId());
	// break;
	// case STORE:
	// SiteStore store = siteStoreService.loadByUserName(userName);
	// idList.add(store.getId());
	// break;
	// }
	// ids = IDCollectionUtils.toString(idList);
	// return ids;
	// }

	// private String getQueryString(String idsStr) {
	// StringBuffer sql = new StringBuffer();
	// sql.append("from " + Comments.class.getName() + " c");
	// sql.append(" where c.id in (select cb.comments.id from ");
	// sql.append(CommentsBridge.class.getName() + " cb");
	// sql.append(" where cb.type=?");
	// sql.append(" and cb.refId in (" + idsStr + ")) and c.domainStatus=?");
	// return sql.toString();
	// }

}
